﻿using GodSoft.Common;
using GodSoft.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GodSoft.Controllers
{
    public class ActionInfoController : BaseController
    {

        GodSoftContext _context = new GodSoftContext();
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Regist(ActionInfo ActionInfo)
        {
            ActionInfo.SubTime = DateTime.Now;
            _context.ActionInfoes.Add(ActionInfo);
            _context.SaveChanges();
            return Content("OK");
        }



        public ActionResult GetAll()
        {
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);


            var temp = from i in _context.ActionInfoes
                       orderby i.ID
                       select new
                       {
                           i.ID,
                           i.ActionName,
                           i.ActionType,
                           i.ParentID,
                           i.RequestUrl,
                           i.RequestHttpType,
                           i.SubTime,
                           i.Describe
                       };
            var list = temp.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            var result = new { total = temp.Count(), rows = list };
            return Json(result, JsonRequestBehavior.AllowGet);

        }

        public ActionResult UpdateInfo(ActionInfo ActionInfo)
        {

            _context.ActionInfoes.Attach(ActionInfo);
            _context.Entry<ActionInfo>(ActionInfo).State = EntityState.Modified;
            _context.SaveChanges();
            return Content("OK");
        }

        public ActionResult DeleteInfo(string deleteInfoID)
        {
            if (string.IsNullOrEmpty(deleteInfoID))
            {
                return Content("请选择您要删除的数据!");
            }
            //截取传递过来的字符串的数字信息
            var idsStr = deleteInfoID.Split(',');

            List<int> deleteIDList = new List<int>();

            foreach (var ID in idsStr)
            {
                deleteIDList.Add(Convert.ToInt32(ID));
            }
            if (DeleteManyInfoes(deleteIDList) > 0)
            {
                return Content("OK");
            }
            else
            {
                return Content("删除失败，请您检查!");
            }

        }

        public int DeleteManyInfoes(List<int> DeleteIDList)
        {
            foreach (var id in DeleteIDList)
            {
                ActionInfo ActionInfo = _context.ActionInfoes.Where(f => f.ID == id).FirstOrDefault();
                _context.ActionInfoes.Remove(ActionInfo);
            }
            return _context.SaveChanges();
        }

    }
}
